<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" width="650px" :title="`${dialogProps.title}员工`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="dialogProps.isView"
      :model="dialogProps.row"
      :hide-required-asterisk="dialogProps.isView"
    >
      <el-form-item label="员工姓名" prop="name">
        <el-input v-model="dialogProps.row!.name" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属部门" prop="departmentId">
        <el-select class="w100" v-model="dialogProps.row!.departmentId">
          <el-option v-for="item in department" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input v-model="dialogProps.row!.mobile" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="dialogProps.row!.sex">
          <el-radio :label="item.value" v-for="item in genderType" :key="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker class="w100" v-model="dialogProps.row!.birthday" type="date" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="dialogProps.row!.nickName" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item label="业余爱好" prop="hobby">
        <el-input v-model="dialogProps.row!.hobby" type="textarea" placeholder="请输入" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false"> 取消 </el-button>
      <el-button v-show="!dialogProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="StaffDialog">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Operation } from "@/api/interface";
import { genderType } from "@/utils/serviceDict";
import { getDepartMentList } from "@/api/modules/operation";

const phoneRules = (rules: any, value: any, callback: any) => {
  if (
    !/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/.test(
      value.trim()
    ) &&
    value
  ) {
    callback(new Error("请输入正确号码"));
  }
  callback();
};

const rules = reactive({
  name: [{ required: true, message: "请输入员工姓名" }],
  departmentId: [{ required: true, message: "请选择部门" }],
  mobile: [
    {
      validator: phoneRules,
      message: "请输入正确的手机号码"
    }
  ]
});

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Operation.ResDepartmentList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const dialogVisible = ref(false);
const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {}
});

const department = ref([]);

// 接收父组件传过来的参数
const acceptParams = (params: DialogProps) => {
  dialogProps.value = params;
  dialogVisible.value = true;
  getDepartMentList({ pageNum: 1, pageSize: 9999 }).then(res => {
    department.value = res.data.list;
  });
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!(dialogProps.value.row);
      ElMessage.success({ message: `${dialogProps.value.title}员工成功！` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
